﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CTRL;
using BLL;
using System.Xml.Linq;
using System.Net;

namespace GUI.UserControls
{
    public partial class UCListMaps : UserControl
    {
        private CtrlMaps ctrlMaps;
        private CtrlDemos ctrlDemos;

        private List<Map> mapList;

        private int currentMapID;

        public UCListMaps()
        {
            InitializeComponent();

            ctrlMaps = CtrlMaps.Instance;
            ctrlDemos = CtrlDemos.Instance;

            mapList = ctrlMaps.GetMapList();
            mapList = mapList.OrderBy(x => x.Name).ToList();

            PopulateList(mapList);
        }

        public void PopulateList(List<Map> list)
        {
            listMaps.Items.Clear();

            foreach (var map in list)
            {
                listMaps.Items.Add(map.Name);
            }
        }

        private void listMaps_SelectedIndexChanged(object sender, EventArgs e)
        {
            var mapName = listMaps.SelectedItem.ToString();

            Map map = mapList.FirstOrDefault(x => x.Name.Equals(mapName));

            if (map != null)
            {
                imgMap.Load(UTIL.KZSettings.MapPictureURL(mapName));

                Record record = ctrlDemos.GetDemoList().FirstOrDefault(x => x.Map.Name.Equals(mapName));

                if (record != null)
                {
                    imgFlag.Image = (Image)GUI.Properties.Resources.ResourceManager.GetObject("flag_" + record.Jumper.Country.TLD);
                    lblJumper.Text = record.Jumper.Name;
                    lblTime.Text = UTIL.KZTime.ToString(record.Time);
                    lblDate.Text = record.Date.ToShortDateString();
                    lblBeaten.Text = record.Beaten.ToString();
                    linkMap.Text = mapName;

                    currentMapID = map.DemoID;

                    listOldDemos.Items.Clear();

                    string url = "https://cosy-climbing.net/e107_files/xml/old_records.php?id=" + map.DemoID;
                    byte[] data;
                    using (WebClient webClient = new WebClient())
                    {
                        data = webClient.DownloadData(url);
                    }

                    string str = Encoding.GetEncoding("UTF-8").GetString(data);

                    if (str.Length > 0)
                    {
                        XDocument xd = XDocument.Parse(str);

                        var list = xd.Descendants("demo").ToList();

                        Imagelist.Images.Clear();

                        foreach (var v in list)
                        {
                            try
                            {
                                Image img = (Image)GUI.Properties.Resources.ResourceManager.GetObject("flag_" + v.Element("country").Value.ToString());
                                Imagelist.Images.Add(v.Element("country").Value.ToString(), img);
                            }
                            catch (Exception)
                            {
                                // not existing image? why so :(
                            }
                        }

                        foreach (var v in list)
                        {
                            ListViewItem lv = new ListViewItem();

                            lv.ImageKey = v.Element("country").Value.ToString();
                            lv.Text = v.Element("jumper").Value.ToString();
                            lv.SubItems.Add(v.Element("time").Value.ToString());
                            lv.SubItems.Add(UTIL.DateTimeFromUNIXTimestamp.UnixTimeStampToDateTime(Double.Parse(v.Element("date").Value.ToString())).ToShortDateString());

                            listOldDemos.Items.Add(lv);
                        }

                        listOldDemos.AutoResizeColumns(System.Windows.Forms.ColumnHeaderAutoResizeStyle.ColumnContent);
                    }
                }
            }
        }

        private void linkMap_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(UTIL.KZSettings.MapURL(currentMapID));
        }
    }
}
